package com.service.business.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.base.service.WalletPoolService;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.WalletEnums;
import com.service.dao.mapper.WalletPoolMapper;
import com.service.dao.model.WalletPool;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class WalletPoolServiceImpl extends ServiceImpl<WalletPoolMapper, WalletPool> implements WalletPoolService {

    @Override
    public WalletPool getCoinsDepositAddress(CoinsEnum coins, WalletEnums.Type type) {

        LambdaQueryWrapper<WalletPool> queryWrapper = new QueryWrapper<WalletPool>().lambda()
                .eq(WalletPool::getCoinsId, coins.getCode())
                .eq(WalletPool::getAddrStatus, WalletEnums.Status.ACTIVE.getCode());

        if(!ObjectUtils.isEmpty(type)){
            queryWrapper.eq(WalletPool::getWalletType, type.getCode());
        }

        List<WalletPool> walletPools = list(queryWrapper);

        if (ObjectUtils.isEmpty(walletPools)){
            //TODO: 是否发送短信给管理员
            return null;
        }

        return walletPools.get((int) (Math.random() * walletPools.size()));
    }
}
